package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.mapper.YgUserBalanceDetailMapper;
import com.ygqh.baby.model.UserBalanceDetailModel;
import com.ygqh.baby.model.YgUserRecommendModel;
import com.ygqh.baby.po.YgUserBalanceDetail;
import com.ygqh.baby.po.YgUserBalanceDetailExample;
import com.ygqh.baby.po.YgUserBalanceDetailExample.Criteria;
import com.ygqh.baby.service.YgUserBalanceDetailService;
import com.ygqh.baby.utils.DateConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class YgUserBalanceDetailServiceImpl implements YgUserBalanceDetailService {

	@Autowired
	private YgUserBalanceDetailMapper ygUserBalanceDetailMapper;

	@Override
	public List<YgUserBalanceDetail> find() {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();

		return ygUserBalanceDetailMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();

		return ygUserBalanceDetailMapper.countByExample(example);
	}

	@Override
	public YgUserBalanceDetail findById(Long id) {
		return ygUserBalanceDetailMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgUserBalanceDetail ygUserBalanceDetail) {
		return ygUserBalanceDetailMapper.insertSelective(ygUserBalanceDetail);
	}

	@Override
	public int update(YgUserBalanceDetail ygUserBalanceDetail) {
		return ygUserBalanceDetailMapper.updateByPrimaryKeySelective(ygUserBalanceDetail);
	}

	@Override
	public ResultSet<YgUserBalanceDetail> search(QueryInfo queryInfo, String remark, String username, Date startDate, Date endDate) {

		List<YgUserBalanceDetail> list = ygUserBalanceDetailMapper.selectBalanceDetailPage(queryInfo, remark, username, startDate, endDate);

		Double total = ygUserBalanceDetailMapper.selectBalanceDetailPageSumprice(username, remark, startDate, endDate);

		int count = ygUserBalanceDetailMapper.selectBalanceDetailPageCount(username, remark, startDate, endDate);
		ResultSet<YgUserBalanceDetail> resultSet = new ResultSet<YgUserBalanceDetail>(count, list);
		resultSet.setExt(total);
		return resultSet;
	}

	@Override
	public ResultSet<YgUserBalanceDetail> findByUserIdPage(QueryInfo queryInfo, Long userId) {
		List<YgUserBalanceDetail> list = ygUserBalanceDetailMapper.selectByCommissionIdPage(queryInfo, userId);
		int count = ygUserBalanceDetailMapper.selectByCommissionIdPageCount(userId);
		ResultSet<YgUserBalanceDetail> resultSet = new ResultSet<YgUserBalanceDetail>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(YgUserBalanceDetail ygUserBalanceDetail) {
		int r = 0;
		if (ygUserBalanceDetail.getId() != null) {
			r = this.update(ygUserBalanceDetail);
		} else {
			r = this.save(ygUserBalanceDetail);
		}
		return r;
	}

	@Override
	public Map<String, Object> getOrderPriceBySourceCode(List<String> sourceCodeList) {

		return ygUserBalanceDetailMapper.getOrderPriceBySourceCode(sourceCodeList);

	}

	@Override
	public YgUserBalanceDetail findRedPacketRechargeByOrderId(Long orderId) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		example.createCriteria().andOrderIdEqualTo(orderId.toString()).andOperationEqualTo("红包转入").andRebateTypeIsNull();
		List<YgUserBalanceDetail> list = ygUserBalanceDetailMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<YgUserBalanceDetail> findDetailByOperationType(Long userId, OperationType operationType, String relationId) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		example.createCriteria().andUserIdEqualTo(userId).andOperationTypeEqualTo(operationType.name()).andOrderIdEqualTo(relationId);
		List<YgUserBalanceDetail> list = ygUserBalanceDetailMapper.selectByExample(example);
		return list;
	}

	@Override
	public List<YgUserBalanceDetail> findDetailByTime(BalanceType balanceType, Date startTime, Date endTime) {

		return ygUserBalanceDetailMapper.selectDetailByTime(balanceType, startTime, endTime);
	}

	@Override
	public int addBalanceDetail(Long userId, BigDecimal money, BalanceType balanceType, OperationType operationType, String remark) {
		YgUserBalanceDetail ygUserBalanceDetail = new YgUserBalanceDetail();
		ygUserBalanceDetail.setUserId(userId);
		if (balanceType.equals(BalanceType.Balance)) {

			ygUserBalanceDetail.setBalancePrice(money);
		} else if (balanceType.equals(BalanceType.PreIncome)) {
			ygUserBalanceDetail.setIncomePayPrice(money);
		}
		ygUserBalanceDetail.setPriceType(balanceType);
		ygUserBalanceDetail.setOperation(operationType.getTitle());
		ygUserBalanceDetail.setOperationType(operationType);
		ygUserBalanceDetail.setCreateTime(new Date());
		ygUserBalanceDetail.setRemark(remark);

		return this.save(ygUserBalanceDetail);
	}

	@Override
	public List<YgUserBalanceDetail> findNoBalanceInComeListByOrderIds(Long[] orderIds) {
		Date sendTime = DateConvertUtils.addDay(DateConvertUtils.getDateStart(new Date()), -15);
		return ygUserBalanceDetailMapper.selectNoBalanceInComeListByOrderIds(orderIds, sendTime);
	}

	@Override
	public int countByOrderId(String orderId, BalanceType balanceType, RebateType rebateType) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		example.createCriteria().andOrderIdEqualTo(orderId).andPriceTypeEqualTo(balanceType.name()).andRebateTypeEqualTo(rebateType.name());
		return ygUserBalanceDetailMapper.countByExample(example);
	}

	@Override
	public List<YgUserBalanceDetail> findDetailByOrderDetail(BalanceType balanceType, RebateType rebateType,
															 Long userId, String orderId, Long orderDetailId) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		Criteria criteria = example.createCriteria();
		if (userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		if (balanceType != null) {
			criteria.andPriceTypeEqualTo(balanceType.name());
		}
		if (StringUtils.isNotBlank(orderId)) {
			criteria.andOrderIdEqualTo(orderId);
		}
		if (orderDetailId != null) {
			criteria.andOrderDetailIdEqualTo(orderDetailId);
		}
		if (rebateType != null) {
			criteria.andRebateTypeEqualTo(rebateType.name());
		}
		return ygUserBalanceDetailMapper.selectByExample(example);
	}

	@Override
	public List<YgUserBalanceDetail> findByOrderDetailId(BalanceType balanceType, RebateType rebateType,
														 Long userId, String orderType, Long orderDetailId) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		Criteria criteria = example.createCriteria();
		if (userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		if (balanceType != null) {
			criteria.andPriceTypeEqualTo(balanceType.name());
		}
		if (StringUtils.isNotBlank(orderType)) {
			BigDecimal zero = new BigDecimal("0.00");
			if (orderType.equals("order")) {
				criteria.andIncomePayPriceGreaterThan(zero);
			} else {

				criteria.andIncomePayPriceLessThan(zero);
			}
		}
		if (orderDetailId != null) {
			criteria.andOrderDetailIdEqualTo(orderDetailId);
		}
		if (rebateType != null) {
			criteria.andRebateTypeEqualTo(rebateType.name());
		}
		return ygUserBalanceDetailMapper.selectByExample(example);
	}

	@Override
	public Boolean exitBalanceDetailByOrderId(BalanceType balanceType, RebateType rebateType, String orderId, Long userId) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		Criteria criteria = example.createCriteria().andUserIdEqualTo(userId);
		if (balanceType != null) {
			criteria.andPriceTypeEqualTo(balanceType.name());
		}
		if (rebateType != null) {
			criteria.andRebateTypeEqualTo(rebateType.name());
		}
		if (StringUtils.isNotBlank(orderId)) {
			criteria.andOrderIdEqualTo(orderId);
		}
		int c = ygUserBalanceDetailMapper.countByExample(example);
		if (c > 0) {
			return true;
		}
		return false;
	}

	@Override
	public List<YgUserBalanceDetail> selectBalanceDetailByOrderId(BalanceType balanceType, RebateType rebateType, Long userId,
																  List<String> orderIdList, String platformNo) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		Criteria criteria = example.createCriteria();
		if (balanceType != null) {
			criteria.andPriceTypeEqualTo(balanceType.name());
		}
		if (rebateType != null) {
			criteria.andRebateTypeEqualTo(rebateType.name());
		}
		if (userId != null) {
			criteria.andUserIdEqualTo(userId);
		}
		if (CollectionUtils.isNotEmpty(orderIdList)) {
			criteria.andOrderIdIn(orderIdList);
		}
		if (StringUtils.isNotBlank(platformNo)) {
			criteria.andPlatformNoEqualTo(platformNo);
		}
		return ygUserBalanceDetailMapper.selectByExample(example);
	}

	@Override
	public List<YgUserBalanceDetail> findBalanceDetailsGroupByOrder(QueryInfo queryInfo, Long userId, BalanceType balanceType) {
		return ygUserBalanceDetailMapper.selectBalanceDetailsGroupByOrder(queryInfo, userId, balanceType);
	}

	@Override
	public List<YgUserBalanceDetail> findBalanceReturnDetailByOrderId(BalanceType balanceType, RebateType rebateType, Long orderId) {
		return ygUserBalanceDetailMapper.findBalanceReturnDetailByOrderId(balanceType, rebateType, orderId);
	}

	@Override
	public int transferBalanceDetail(Long sourceUserId, Long targetUserId) {
		YgUserBalanceDetailExample example = new YgUserBalanceDetailExample();
		example.createCriteria().andUserIdEqualTo(sourceUserId);

		YgUserBalanceDetail record = new YgUserBalanceDetail();
		record.setUserId(targetUserId);
		return ygUserBalanceDetailMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int updateIsGived(Boolean isGived, List<Long> idlList) {
		if (CollectionUtils.isEmpty(idlList)) {
			return 0;
		}
		return ygUserBalanceDetailMapper.updateIsGived(isGived, idlList);
	}

	@Override
	public int updateIsGivedByOrderIdList(Boolean isGived, List<String> orderIdList) {
		if (CollectionUtils.isEmpty(orderIdList)) {
			return 0;
		}
		return ygUserBalanceDetailMapper.updateIsGivedByOrderIdList(isGived, orderIdList);
	}

	@Override
	public Map<String, Object> findMyRebate(Long userId, String platformNo) {
		return ygUserBalanceDetailMapper.selectMyRebate(userId, platformNo);
	}

	@Override
	public List<YgUserRecommendModel> statisticsRecommendPrice(Long userId, List<Long> recommendUserIdList,
															   String platformNo) {
		return ygUserBalanceDetailMapper.statisticsRecommendPrice(userId, recommendUserIdList, platformNo);
	}

	@Override
	public List<UserBalanceDetailModel> findDetailList(QueryInfo queryInfo, Long userId, Date limitTime,
													   String platformNo, BalanceType balanceType) {
		return ygUserBalanceDetailMapper.selectDetailList(queryInfo, userId, limitTime, platformNo, balanceType);
	}
}
